/* eslint-disable */
console.log("tax injected");
import { autoInputAccount } from "./options/autoLogin.js"
import { cwbb } from "./options/cwbb.js"
import { fillHspj } from "./options/hsqj.js"
import { waitTimeOut } from "../common.js"
import { fillInputDone, getBaseInfo, backNationData } from "../message.js"

window.baseInfo = {};
let isGetInfo = false;
$(window).load(() => {
  isGetInfo = true;
  getBaseInfo()
})
setTimeout(() => {
  if (!isGetInfo) {
    getBaseInfo()
  }
}, 5000);



window.addEventListener(
  "message",
  async function ({ data }) {
    // 只监听来自于content script的信息
    if (data.sender !== "content-script") {
      return false;
    }
    // 登录填写基本数据
    if (data.event === "getBaseInfo") {
      window.baseInfo = data.data;
      autoInputAccount(data.data);
      return false;
    }
    if (data.event === "setSaasData") {
      data = data.data;
      let sheet = data.saasData.sheet;
      if (sheet.includes('hsqj')) {
        try {
          fillHspj(data, sheet);
          fillInputDone({ success: true, tax: 'hsqj', info: '' })
          return true;
        } catch (error) {
          fillInputDone({ success: false, tax: 'hsqj', info: error.message })
        }
      } else if (['balance', 'cash', 'profit'].includes(sheet)) {
        cwbb(data);
        fillInputDone({ success: true, tax: 'cwbb', info: '填报成功' })
      }
    }
    if (data.event === "getNationData") {
      window.baseInfo = data.data.baseInfo;
      await getNationData(data.data.baseInfo);
    }
  },
  false
);


/**
 * 获取公司信息
 */
async function getNationData(baseInfo) {
  let { institutionCode } = baseInfo.companyInfo
  let taxInfo = {
    institution: institutionCode === "1" ? '2007' : '2013', // 国税对应制度 2007 | 2013
    nsrsbh: "", // 纳税人申报号
    nsrmc: "", // 纳税人名称（纳税公司）
    skssqq: "", // 税款所属期起
    skssqz: "", // 税款所属期止
    saasInstitution: institutionCode, // 国税对应制度 2007 | 2013
  };
  backNationData({
    taxInfo: taxInfo,
  })
}